package cn.zd.demo.flink.kafka2db;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Stream;

public class StreamDemoTest {
    public static void main(String[] args) {
        int data_len = 10;
        List<List<A>> aaList = new ArrayList<>();
        for (int j = 0; j < data_len; j++) {
            List<A> aList = new ArrayList<>();
            for (int i = 0; i < data_len; i++) {
                A a = new A();
                a.setNum(i);
                a.setA(UUID.randomUUID().toString());
                aList.add(a);
            }
            aaList.add(aList);
        }
        Stream<List<A>> stream1 = aaList.stream();
        //map 将每一项进行映射处理 返回映射后的对象
        // <R> Stream<R> map(Function<? super T, ? extends R> mapper);
        Stream<B> bStream1 = stream1.map(a -> {
            // a是stream1中的每一项，也就是List<A>
            StringBuilder sb = new StringBuilder();
            a.forEach(a1 -> {
                sb.append(a1.getNum()+":"+a1.getA()+":"+a1.getB());
            });
           return new B(a.size(),sb.toString());
        });
        System.out.println("map");
        bStream1.forEach(System.out::print);
        //flatMap 将每一项展开处理 返回流，可以一对一、一对多处理 简单理解可以拆包成流又可以很方便的如合并处理
        // <R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper);
        stream1 = aaList.stream();
        //Stream<A> bStream2 = stream1.flatMap((Function<List<A>, Stream<A>>) as -> as.stream());
        Stream<A> bStream2 = stream1.flatMap(new Function<List<A>, Stream<A>>() {
            @Override
            public Stream<A> apply(List<A> as) {
                return as.stream();
            }
        });
        System.out.println("flatMap");
        bStream2.forEach(System.out::print);
    }
}
class A{
    private int num;
    private String a;
    private String b;

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }

    public String getB() {
        return b;
    }

    public void setB(String b) {
        this.b = b;
    }

    @Override
    public String toString() {
        return "A{" +
                "num=" + num +
                ", a='" + a + '\'' +
                ", b='" + b + '\'' +
                "}\n";
    }
}
class B{

    public B(int size,String s) {
        this.size = size;
        this.s = s;
    }
    private int size;
    private String s;

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public String getS() {
        return s;
    }

    public void setS(String s) {
        this.s = s;
    }

    @Override
    public String toString() {
        return "B{" +
                "size=" + size +
                ", s='" + s + '\'' +
                "}\n";
    }
}
class As{
    private List<A> as;

    public List<A> getAs() {
        return as;
    }

    public void setAs(List<A> as) {
        this.as = as;
    }
}
